home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d2 / vampire2.arc / BIN2HEX.C next >
Text File  |  1989-04-28  |  4KB  |  176 lines

  1. /*
  2.     Binary to HEX-ASCII utility
  3.  
  4.     Makes a binary file into a HEX file, 32 bytes/block with
  5.     proper sumcheck byte at the end. The starting address for the
  6.     HEX file is settable; the input is assumed to be contiguous
  7.     locations. Some garbage may be included at the end of the
  8.     HEX file since the program does not recognize 0x1A as an
  9.     end-of-file code in the binary input.
  10.  
  11.     Called with:
  12.         bin2hex [<binary file 1> [<bin. file 2>...]]
  13.  
  14. Original version written by:
  15.     Robert Pasky
  16.     36 Wiswall Rd
  17.     Newton Centre, MA 01259
  18. Modified by Edward I. Comer
  19. NOTE: The file opens are modified for C86
  20. */
  21.  
  22. #include "stdio.h"
  23. #define DONE    0
  24. #define MORE    1
  25.  
  26.  
  27. int i, j, fd;
  28. char doswath = 0;
  29. unsigned int addr,sad,offset,swath;
  30. char fnbuf[30], *fname;        /* filename buffer & ptr */
  31. char onbuf[30], *oname;        /* output filename buf & ptr */
  32. FILE *ifp, *ofp;    /* file pointers */
  33. char hextab[16];
  34. char kybdbuf[20];
  35. char sumcheck;
  36.  
  37. main(argc,argv)
  38. char **argv;
  39. {
  40.     strcpy(hextab,"0123456789ABCDEF");
  41.  
  42.     while (1)
  43.     {
  44.         doswath = 0;
  45.         oname = onbuf;
  46.         if (argc-1)
  47.         {
  48.             fname = *++argv;
  49.             argc--;
  50.         }
  51.         else
  52.         {
  53.             printf ("\nEnter binary file name: ");
  54.             if (!*(fname = gets(fnbuf))) break;
  55.         }
  56.         strcpy (oname,fname);
  57.         striptype(oname);
  58.         strcat (oname,".HEX");
  59.  
  60.         if((ifp=fopen(fname,"rb")) == NULL)
  61.         {
  62.             printf("Can't open %s\n",fname);
  63.             continue;
  64.         }
  65.         else    printf("\nReading %-13s",fname);
  66.         if((ofp=fopen(oname,"wb")) == NULL)
  67.         {
  68.             printf("Can't create %s\n",fname);
  69.             continue;
  70.         }
  71.         else    printf ("\nWriting %-13s",oname);
  72.         printf ("\nStarting address? ");
  73.         sscanf (gets(kybdbuf),"%x",&addr);
  74.         sad = addr;    /* save start address */
  75.         printf("\nOffset into %s (in hex bytes) ?",fname);
  76.         sscanf(gets(kybdbuf),"%x",&offset);
  77.         printf("\nSwath size OR 0 (in hex bytes) ?");
  78.         sscanf(gets(kybdbuf),"%x",&swath);
  79.         if(swath != 0)
  80.             doswath = 1;
  81.         /*
  82.         ** offset into file by desired amount
  83.         */
  84.         while(offset)
  85.         {
  86.             getc(ifp);    /* discard byte */
  87.             offset--;
  88.         }
  89.         while(doblock())
  90.             ;
  91.         putc(0xD,ofp);
  92.         putc(0xA,ofp);
  93.         fclose(ifp);
  94.         fclose(ofp);
  95.         printf ("\nConversion complete\n");
  96.         printf ("\nStart address: %4x",sad);
  97.         printf ("\nEnd address:   %4x",addr);
  98.     }
  99. }
  100.  
  101. striptype(s)
  102. char *s;
  103. {
  104.     while (*s) {
  105.         if (*s == '.') *s = '\0';
  106.         else s++;
  107.     }
  108. }
  109.  
  110. /*    Send a HEX block --
  111.     cr,lf,':',count,address,filler,32 bytes of code,sumcheck
  112.     return eofflag -- DONE if  end of file reached
  113. */
  114. doblock()
  115. {
  116.     int eofflag,c;
  117.  
  118.     putc(0xD,ofp); 
  119.     putc(0xA,ofp);
  120.     putc(':',ofp);
  121.     sumcheck = 0;
  122.     putbyte(0x20,ofp);
  123.     putaddr(addr,ofp);
  124.     putbyte(0,ofp);
  125.     eofflag = MORE;
  126.  
  127.     for (i=0; i<0x20; i++)
  128.     {
  129.         if ((c = getc(ifp)) == EOF || (doswath && swath == 0))
  130.         {
  131.             eofflag = DONE;
  132.             c = 0;
  133.         }
  134.         putbyte(c,ofp);
  135.         if(doswath)
  136.             swath--;
  137.     }
  138.     putbyte(-sumcheck,ofp);
  139.     addr += 0x20;
  140.     if (eofflag == DONE)
  141.     {
  142.         putc(0xD,ofp); 
  143.         putc(0xA,ofp); 
  144.         putc(':',ofp);
  145.         putbyte(0,ofp);    /* count of 0 is eof signal */
  146.         putaddr(0,ofp);
  147.         putbyte(0,ofp);
  148.     }
  149.     return (eofflag);
  150. }
  151.  
  152. /*    Send 1 byte (2 characters);
  153.     value of h to file iofp
  154. */
  155. putbyte(h,iofp)
  156. int h;
  157. FILE *iofp;
  158. {
  159.     sumcheck += h;
  160.     putc( hextab[(h>>4)&0xF], iofp);
  161.     putc( hextab[h&0xF], iofp);
  162. }
  163.  
  164. /*    Send 2 bytes (4 characters)
  165.     address    value of h to file iofp
  166. */
  167. putaddr(h,iofp)
  168. int h;
  169. FILE *iofp;
  170. {
  171.     putbyte(((h>>8) & 0xFF), iofp);
  172.     putbyte(h & 0xFF, iofp);
  173. }
  174.  
  175.  
  176.